package com.linkoog.devtools.intellij.ui;

import com.linkoog.devtools.code_generator.util.StringUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.function.Consumer;

public class IconPlaceholderJTextField extends JTextField {

    private Icon icon;
    private String placeholder;
    private Consumer<String> finishCallback;


    public IconPlaceholderJTextField(Icon icon, String placeholder, Consumer<String> finishCallback) {
        if(StringUtil.isBlank(placeholder)){
            placeholder = "";
        }

        this.icon = icon;
        this.placeholder = placeholder;
        this.finishCallback = finishCallback;


        int gapLeft = 5;

        Insets borderInsets = getInsets();
        Insets marginInsets;
        if (icon == null){
            marginInsets = new Insets(0, gapLeft + borderInsets.left, 0, 0);
        } else {
            marginInsets = new Insets(0, gapLeft + (borderInsets.left + icon.getIconWidth()) * 3  , 0, 0);
        }


        this.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        this.setMargin(marginInsets);
        this.addFocusListener(new PlaceholderTextFieldAdapter());

        /*ActionListener actionListener = e -> {};
        this.registerKeyboardAction(actionListener,
                KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
                JComponent.WHEN_FOCUSED);

        this.registerKeyboardAction(actionListener,
                KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),
                JComponent.WHEN_FOCUSED);*/
    }


    @Override
    public void paintComponent(Graphics graphics) {
        Insets borderInsets = getInsets();

        int gapLeft = 5;
        Insets marginInsets;
        if (icon == null){
            marginInsets = new Insets(0, gapLeft + borderInsets.left, 0, 0);
        } else {
            marginInsets = new Insets(0, gapLeft + (borderInsets.left + icon.getIconWidth()) * 2  , 0, 0);
        }

        this.setMargin(marginInsets);
        super.paintComponent(graphics);
        if (icon != null) {
            int iconWidth = icon.getIconWidth();
            int iconHeight = icon.getIconHeight();
            int Height = this.getHeight();
            //在文本框中画上之前图片
            System.out.println("iconWidth:" + iconWidth);

            System.out.println("x:" + (borderInsets.left - iconWidth) / 2);
            icon.paintIcon(this, graphics, (borderInsets.left + iconWidth) / 2, (Height - iconHeight) / 2);
        }
    }


    private class PlaceholderTextFieldAdapter implements FocusListener{

        public PlaceholderTextFieldAdapter() {
            setText(placeholder);
        }

        @Override
        public void focusGained(FocusEvent e) {
            String tempString = getText();
            if (tempString.equals(placeholder)) {
                //textField.setForeground(Color.BLACK);
                setText("");
            }
        }

        @Override
        public void focusLost(FocusEvent e) {
            String tempString = getText();
            //textField.setForeground(Color.GRAY);
            setText(placeholder);
        }
    }


}
